package com.larissa.android.quizdemo;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.larissa.android.quizdemo.databinding.ActivityHelpBinding;

public class HelpActivity extends AppCompatActivity{
    private static final String TAG="HelpActivity";
    private static final String CLICK_KEY="CLICK";
    private static final String ANSWER_KEY="ANSWER_KEY";
    private static final String RESULT_KEY="RESULT_KEY";
    private ActivityHelpBinding mActivityHelpBinding;

    private boolean mAnswer;
    private boolean mHasClicked;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate() called");
        mActivityHelpBinding=ActivityHelpBinding.inflate(getLayoutInflater());
        setContentView(mActivityHelpBinding.getRoot());

        // 给HelpActivity的ActionBar指定title
        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle(TAG);

        mActivityHelpBinding.btnShowAnswer.setOnClickListener(view -> {
            showAnswer();
        });
        // 使用key值从intent中获取答案数据
        mAnswer=getIntent().getBooleanExtra(ANSWER_KEY,true);

        if(savedInstanceState!=null){
            mHasClicked=savedInstanceState.getBoolean(CLICK_KEY);
            if(mHasClicked)
                showAnswer();
        }

    }

    private void showAnswer(){
        mActivityHelpBinding.txAnswer.setText(String.valueOf(mAnswer));
        mActivityHelpBinding.btnShowAnswer.setEnabled(false);
        mHasClicked=true;
        setActivityResult();
    }

    @Override
    protected void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putBoolean(CLICK_KEY,mHasClicked);
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    private void setActivityResult(){
        Log.d(TAG,"setActivityResult() called");
        Intent intent =new Intent();
        intent.putExtra(RESULT_KEY,mHasClicked);
        setResult(RESULT_OK,intent);
    }

}